package com.hsgene.order.controller.agency;

import com.hsgene.common.util.resolver.FromSession;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.web.session.UserInfo;
import com.hsgene.order.condition.AgencyOrderCondition;
import com.hsgene.order.domain.OrderPackageDetail;
import com.hsgene.order.domain.SendReport;
import com.hsgene.order.dto.agency.AgencyOrderDetailDto;
import com.hsgene.order.dto.agency.SampleConfirmDto;
import com.hsgene.order.exception.OrderErrorCode;
import com.hsgene.order.service.AgencyOrderService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 机构订单接口
 * @author: 江承良
 * @date: 2018/9/20 10:48
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/agency")
public class AgencyOrderController {
    private Logger logger = LogManager.getLogger(AgencyOrderController.class);

    @Autowired
    private AgencyOrderService agencyOrderService;

    /**
     * 机构订单列表
     *
     * @param page 页码
     * @param size 每页大小
     * @param condition 机构订单查询条件
     * @return
     */
    @GetMapping("/orders")
    public ApiResult agencyOrders(Integer page,
                                  Integer size,
                                  AgencyOrderCondition condition,
                                  @FromSession UserInfo userInfo) {
        try {
            String agencyId = null;
            //如果用户不是平台用户则需要具体的机构id
            if (userInfo != null && !userInfo.getExtra().getSystemSuper() && "11".equals(userInfo.getUserType())) {
                agencyId = userInfo.getExtra().getCompanyId();
            } else if (userInfo == null) {
                return new ApiResult(5001, "failed", "user is not exist", "用户信息获取失败，请尝试刷新或重新登录");
            }
            LimitHelper limitHelper = LimitHelper.create(page, size, true);
            return agencyOrderService.queryAgencyOrders(condition, agencyId, limitHelper);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询机构订单列表出现异常", e);
            return ApiResult.fail(OrderErrorCode.AGENCY_ORDER_ERROR);
        }
    }

    /**
     * 机构订单管理——申请单查询
     *
     * @param geneticTestingOrderId 订单id
     * @return
     */
    @GetMapping("order/requisition/{genetic_testing_order_id}")
    public ApiResult agencyOrderRequisition(@PathVariable("genetic_testing_order_id") String geneticTestingOrderId) {
        try {
            ApiResult apiResult = agencyOrderService.queryAgencyOrderRequisition(geneticTestingOrderId);
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(OrderErrorCode.AGENCY_REQUISITION_ERROR);
        }
    }


    /***
     *
     * 机构确认样本弹窗信息获取
     * @author jcl
     * @date 2018/11/8 9:34
     * @param orderId 订单id
     * @param packageId 套餐id
     * @return
     */
    @GetMapping("order/sample/confirm")
    public ApiResult<SampleConfirmDto> sampleConfirmInfo(String orderId, String packageId) {
        return agencyOrderService.sampleConfirmInfo(orderId, packageId);
    }

    /**
     * 机构确认样本
     *
     * @param sampleConfirmDto
     * @return
     * @author jcl
     * @date 2018/11/8 13:42
     */
    @PutMapping("order/sample/confirm")
    public ApiResult<NullObject> sampleConfirm(@RequestBody SampleConfirmDto sampleConfirmDto, @FromSession UserInfo userInfo) {
        return agencyOrderService.sampleConfirm(sampleConfirmDto, userInfo.getUserId());
    }

    /**
     * com.hsgene.order.domain.v1.GeneticTestingOrderPackageSend
     * 机构上传报告
     *
     * @param orderId
     * @param orderPackageDetail
     * @param userInfo
     * @return
     * @author FX
     * @date 2018/11/7 14:13
     */
    @PutMapping("order/{orderId}/upload/report")
    public ApiResult uploadReport(@PathVariable("orderId") String orderId, @RequestBody OrderPackageDetail orderPackageDetail, @FromSession UserInfo userInfo) {
            orderPackageDetail.setGeneticTestingOrderId(orderId);
            orderPackageDetail.setUploader(userInfo.getUserId());
        return agencyOrderService.uploadReport(orderPackageDetail);
    }

    /**
     * 寄送报告
     *
     * @param orderId
     * @param sendReport
     * @return
     * @author FX
     * @date 2018/11/9 14:02
     */
    @PutMapping("order/{orderId}/send/report")
    public ApiResult sendAReport(@PathVariable("orderId") String orderId, @RequestBody SendReport sendReport, @FromSession UserInfo userInfo) {
        try {
            sendReport.setOrderId(orderId);
            String userId = "";
            if (userInfo != null) {
                userId = userInfo.getUserId();
            }
            return agencyOrderService.sendAReport(sendReport, userId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("机构寄送出现异常");
            return new ApiResult(5001, "failed", "寄送物品出现异常，请检查输入是否正确", null);
        }
    }


    /***
     * 机构订单详情
     * @author jcl
     * @date 2018/11/12 9:57
     * @param orderId 订单id
     * @param packageId 套餐id
     * @return
     */
    @GetMapping("order/{order_id}/{package_id}")
    public ApiResult<AgencyOrderDetailDto> agencyOrderDetail(@PathVariable("order_id")String orderId, @PathVariable("package_id")String packageId){
        try {
            return agencyOrderService.agencyOrderDetail(orderId, packageId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("机构订单详情出现异常");
            return new ApiResult(500, "failed", "机构订单详情出现异常", e.toString());
        }
    }
}
